package com.dcy.znoa.controller.sys;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.sys.model.role.AddRoleModel;
import com.dcy.znoa.controller.sys.model.role.SelectRoleModel;
import com.dcy.znoa.controller.sys.model.role.UpdateRoleModel;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.sys.Role;
import com.dcy.znoa.service.sys.RoleService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

/**
 * @author chenjie
 */
@Controller
@RequestMapping("role")
public class RoleController extends BaseController {
    @Autowired
    RoleService roleService;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(SelectRoleModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
        Oper oper = getCurrentUser();
        //查询所属公司的角色
        model.setCompId(oper.getCompId());

        return roleService.selectList(model);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddRoleModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            FieldError fieldError= bindingResult.getFieldError();
            return  new JsonCommon("404",fieldError.getDefaultMessage());
        }
        if(model.getRoleName().length()>50){
            return  new JsonCommon("404","角色名称长度不能大于50");
        }
        if(model.getDescribe()!=null && model.getDescribe().length()>50){
            return  new JsonCommon("404","角色描述长度不能大于50");
        }
        Oper oper = getCurrentUser();
    	model.setCreateOperId(oper.getOperId());
        model.setCompName(oper.getCompany().getCompName());
        model.setCompId(getCurrentUser().getCompId());
        
        Role role = new Role();
        BeanUtils.copyProperties(role, model);
        return roleService.insert(role);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    //修改、禁用、启用
    public JsonCommon updateById(@Valid UpdateRoleModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            FieldError fieldError= bindingResult.getFieldError();
            return  new JsonCommon("404",fieldError.getDefaultMessage());
        }
        if(model.getRoleName()!=null && model.getRoleName().length()>50){
            return  new JsonCommon("404","角色名称长度不能大于50");
        }
        if(model.getDescribe()!=null && model.getDescribe().length()>50){
            return  new JsonCommon("404","角色描述长度不能大于50");
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
    	Role role = new Role();
        BeanUtils.copyProperties(role, model);
        return roleService.updateById(role);
    }


    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectUserListByRoleId")
    //只需要roleId和分页参数
    public JsonForList selectUserListByRoleId(SelectRoleModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
        return roleService.selectUserListByRoleId(model);
    }
    
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectUserListNotOfRoleId")
    //查找没有此角色的用户：此角色的公司；只需要roleId和分页参数
    public JsonForList selectUserListNotOfRoleId(SelectRoleModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
        return roleService.selectUserListNotOfRoleId(model);
    }
}
